<?php
function curl_multi_post($urls,$post) {
    $queue = curl_multi_init();
    $map = array();
    foreach ($urls as $key => $url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post[$key]);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_multi_add_handle($queue, $ch);
        $map[(string) $ch] = $url;
    }
    $responses = array();
    do {
        while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) ;
        if ($code != CURLM_OK) { break; }
        while ($done = curl_multi_info_read($queue)) {
            $error = curl_error($done['handle']);
            echo $results = curl_multi_getcontent($done['handle']);
			$res = json_decode($results,true);
			/*
			if($res["errcode"]!=0){
				continue;
			}
			*/
            $responses[$map[(string) $done['handle']]] = compact('error', 'results');
            curl_multi_remove_handle($queue, $done['handle']);
            curl_close($done['handle']);
        }
        if ($active > 0) {
            curl_multi_select($queue, 0.5);
        }
    } while ($active);
    curl_multi_close($queue);
    return $responses;
}

 function postData($url,$data){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $tmpInfo = curl_exec($ch);
      if (curl_errno($ch)) {
        return curl_error($ch);
      }
      curl_close($ch);
      return $tmpInfo;
    }

function get_access_token(){
	$appid="wxa8d16e1cf11c40a7";
$appsecret="ec585265ee1bab6ebd8b6a51865db503";
  $result = json_decode(file_get_contents("access_token.json"),true);

  if( $result['expires_in']  < time() ){
	  echo "updata token<br>";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
		$getContent=file_get_contents($url);
        $res = json_decode($getContent,true);
        $access_token = $res['access_token'];   //重新获取的access_token
        if($access_token){
            $reflesh['expires_in'] = time()+7200;
            $reflesh['access_token'] = $access_token;
            $fp = fopen("access_token.json", "w");
            fwrite($fp, json_encode($reflesh));
            fclose($fp);
        }
    }else{
      $access_token = $result['access_token'];
    }
    return $access_token;
}

function force_get_access_token(){
  $result = json_decode(file_get_contents("access_token.json"),true);
$appid="wxa8d16e1cf11c40a7";
$appsecret="ec585265ee1bab6ebd8b6a51865db503";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
		$getContent=file_get_contents($url);
        $res = json_decode($getContent,true);
        $access_token = $res['access_token'];   //重新获取的access_token
        if($access_token){
            $reflesh['expires_in'] = time()+7000;
            $reflesh['access_token'] = $access_token;
            $fp = fopen("access_token.json", "w");
            fwrite($fp, json_encode($reflesh));
            fclose($fp);
        }

    return $access_token;
}
//echo force_get_access_token();